package org.zjt.mr.wordcnt;

import org.apache.hadoop.io.IntWritable;
import org.apache.hadoop.io.LongWritable;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapreduce.Mapper;
import org.apache.hadoop.util.StringUtils;

import java.io.IOException;

public class WordCountMapper extends Mapper<LongWritable, Text, Text, IntWritable> {

    //该方法循环调用，从文件的split中读取每行调用一次，把该行首个单词所在的下标为key，该行的内容为value         key为20，则是第二行第一个首字母排序，是整个文档的第二是字母。
    protected void map(LongWritable key, Text value, Context context)
            throws IOException, InterruptedException {
        String[] words = StringUtils.split(value.toString(), ' ');
        for (String w : words) {
            context.write(new Text(w), new IntWritable(1));
        }
    }
}
